<template>
  <div class="login">
    <div class="phone">
      <div class="top">
        <img src="@/assets/imgs/login-logo.png" alt />
        <span class="t1">黑马面面</span>
        <div class="line">|</div>
        <span class="t2">用户登录</span>
      </div>
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item prop="phone">
          <el-input v-model="form.phone" prefix-icon="el-icon-phone" placeholder="请输入手机号"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="form.password"
            placeholder="请输入密码"
            show-password
            prefix-icon="el-icon-lock"
          ></el-input>
        </el-form-item>
        <el-form-item prop="code">
          <el-row>
            <el-col :span="16">
              <el-input v-model="form.code" prefix-icon="el-icon-key" placeholder="请输入验证码"></el-input>
            </el-col>
            <el-col :span="8">
              <img @click="getimgs" class="code" :src="codeimg" alt />
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item prop="checked">
          <el-checkbox v-model="form.checked">
            我已阅读并同意
            <el-link type="primary">用户协议</el-link>及
            <el-link type="primary">隐私条款</el-link>
          </el-checkbox>
        </el-form-item>
        <el-form-item>
          <el-button @click="login" class="btn" type="primary">登录</el-button>
          <el-button class="btn" @click="resigter" type="primary">注册</el-button>
        </el-form-item>
      </el-form>
      <!-- 弹出注册页面 -->
      <!-- <el-dialog :visible.sync="isShow" :show-close="false">
        <el-form></el-form>
      </el-dialog>-->
    </div>
    <resigter ref="resigter"></resigter>
    <img src="@/assets/imgs/login-right.png" alt />
  </div>
</template>

<script>
// import axios from "axios";
import resigter from "./resigter.vue";
import { tologin } from "@/api/login.js";
import { settoken, gettoken } from "../../untils/local.js";
export default {
  components: {
    resigter,
  },
  data() {
    return {
      // isShow: false,
      baseURL: process.env.VUE_APP_URL,
      codeimg: "http://127.0.0.1/heimamm/public/captcha?type=login",
      form: {
        phone: "",
        password: "",
        code: "",
        checked: "",
      },
      rules: {
        phone: [
          {
            required: true,
            message: "请输入手机号",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              let _deg = /^(0|86|17951)?(13[0-9]|15[012356789]|166|17[3678]|18[0-9]|14[57])[0-9]{8}$/;
              if (_deg.test(value)) {
                callback();
              } else {
                callback(new Error("请输入正确的手机号"));
              }
            },
          },
        ],
        password: [
          {
            required: true,
            message: "请输入密码",
            trigger: "blur",
          },
          { min: 3, max: 9, message: "长度在3-9个字符", trigger: "blur" },
        ],
        code: [
          {
            required: true,
            message: "请输入验证码",
            trigger: "blur",
          },
        ],
        checked: [
          {
            required: true,
            message: "请输入勾选协议",
            trigger: "blur",
          },
          {
            validator: (rule, value, callback) => {
              if (value) {
                callback();
              } else {
                callback(new Error("请勾选"));
              }
            },
          },
        ],
      },
    };
  },
  created() {
    if (gettoken("token")) {
      this.$router.push("/layout");
    }
  },
  methods: {
    getimgs() {
      this.codeimg = this.baseURL + "/captcha?type=login&" + Date.now();
    },
    login() {
      this.$refs.form.validate((res) => {
        if (res) {
          tologin(this.form).then((res) => {
            // console.log(res);
            settoken("token", res.data.data.token);
            this.$router.push("/layout");
          });
          this.$message.success("成功登录");
        } else {
          this.$message.error("请填写全部信息");
        }
      });
    },
    resigter() {
      this.$refs.resigter.isShow = true;
    },
  },
};
</script>

<style lang='less' >
.login {
  width: 100%;
  height: 100%;
  background: linear-gradient(#1493fa 28%, #01c6fa 96%);
  display: flex;
  align-items: center;
  justify-content: space-around;
}
.phone {
  width: 485px;
  height: 550px;
  background-color: #fff;
  margin: 120px 73px;
  padding: 0 45px;
  box-sizing: border-box;
  .top {
    display: flex;
    align-items: center;
    padding: 42px 0;
    img {
      width: 20px;
      height: 17px;
    }
    .t1 {
      font-size: 22px;

      text-align: center;
      margin: 0 12px;
    }
    .t2 {
      font-size: 22px;
      margin: 0 12px;
    }
    .line {
      font-size: 22px;
    }
  }
  .code {
    width: 100%;
    height: 40px;
  }
  .btn {
    display: block;
    width: 100%;
    margin-bottom: 10px;
  }
  .el-button + .el-button {
    margin-left: 0;
  }
}
</style>